package com.re6g3y.game.Tile.Hexagonal.mapGenerate;

import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.math.Vector2;
import com.re6g3y.game.Tile.Hexagonal.HexagonalIterator;
import com.re6g3y.game.Tile.Hexagonal.HexagonalMap;
import com.re6g3y.game.Tile.Hexagonal.HexagonalTile;
import com.re6g3y.game.Tile.Hexagonal.QueryCallback;

import java.util.*;

public class HexagonalMapEditor {
    private HexagonalMap map;
    private int size=100;
    private int seed;
    public HexagonalMapEditor(int seed){
        this.seed=seed;
        this.map=new HexagonalMap();
    }

    public final List<HexagonalTile> mapList =new ArrayList<HexagonalTile>((size+1)*100*3+1);
    private final List<HexagonalTile> tmpList =new ArrayList<HexagonalTile>((size+1)*100*3+1);

    final Color colors[]={Color.CYAN,Color.PINK,Color.LIME,Color.VIOLET,Color.SCARLET,
            Color.MAGENTA,Color.DARK_GRAY,Color.CLEAR,Color.GOLD, Color.ROYAL
    };

    public List<HexagonalTile> getTileByRange(Vector2 center,int range){
        mapList.clear();
        map.queryByPointAndRange(center, new QueryCallback() {
            int ind=0;
            Vector2 tmp=new Vector2();
            @Override
            public boolean Callback(HexagonalIterator iterator) {
//                Object ent=map.getEntities(iterator);
//                if(ent==null) {

                    HexagonalTile tile;
                    map.getTileCenter(iterator,tmp);
                    if(tmpList.size()<=ind) {
                        tile = new HexagonalTile(tmp, iterator.getVal(),HexagonalMap.innerRadius);
                        tmpList.add(tile);
                        ind++;
                    }else{
                        tile =tmpList.get(ind++);
                        tile.getSprite().setCenter(tmp.x,tmp.y);
                    }
                    tile.property = GenerateChunk.getTileHeight(iterator.getPositionCenterX(), iterator.getPositionCenterY(), seed);
                    drawColor(tile);
                mapList.add(tile);
//                    map.setEntity(tile,tile.getIterVal());
//                }else {
//                    mapList.add((HexagonalTile) ent);
//                }
                return false;
            }
        }, 0, range);
        return mapList;
    }

    public List<HexagonalChunk> getChunkByRange(final Vector2 center,int range){
        final ArrayList<HexagonalChunk> chunks=new ArrayList<HexagonalChunk>(30);
        final float rotate=-27.465f;
        center.rotate(-rotate);
        center.set(center.x*0.975f,center.y*0.975f);
        map.queryByPointAndRange(center, new QueryCallback() {
            @Override
            public boolean Callback(HexagonalIterator iterator) {
                HexagonalChunk  chunk=(HexagonalChunk) map.getEntities(iterator);;
                if(chunk==null) {
                    Vector2 cent=iterator.getPositionCenter();
                    cent.rotate(rotate);
                    chunk=new HexagonalChunk(cent);
                    map.setEntity(chunk,iterator.getVal());
                }
                chunks.add( chunk);
                return false;
            }
        }, 0, range);
        return chunks;
    }

    final static Color darkGreen=Color.GREEN.cpy().mul(0.85f,0.85f,0.85f,1f);
    public static void drawColor(HexagonalTile tile){
            float val=tile.property*tile.property*tile.property;
        //             val*=val;
//            if(val<0.3)val=1;
//            else val=0;
            if (tile.property >= 0.85f) {
                tile.getSprite().setColor(Color.WHITE);
            } else if (tile.property >= 0.75f) {
                tile.getSprite().setColor(Color.SLATE);
            } else if (tile.property >= 0.60f) {
                tile.getSprite().setColor(darkGreen);
            } else if (tile.property >= 0.3f) {
                tile.getSprite().setColor(Color.GREEN);
            } else if (tile.property >= 0.25f) {
                tile.getSprite().setColor(Color.GOLD);
            } else {
                tile.getSprite().setColor(Color.ROYAL);
            }
            tile.getSprite().setColor(darkGreen.set(val,val,val,1f));
//            if(tile.property>0.45f&&tile.property<0.5f){
//                tile.getSprite().setColor(Color.ROYAL);
//            }
    }
}
